﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace DoAn
{
    public partial class frmDanhsachkhachhang : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
       public frmDanhsachkhachhang()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }
       private void frmDanhsachkhachhang_Load(object sender, EventArgs e)
       {
          
       }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            String makh = tbmkh.Text;
            String khten = tbtkh.Text;
            String khngaysinh = dtns.Value.ToString();
            String khdiachi = tbdc.Text;
            String khsdt = tbsdt.Text;
            String khgioitinh = cbgt.Text;
            String khemail = tbemail.Text;
            String khcmt = tbcmt.Text;
            String khngaytaohoso = dtngaytao.Value.ToString();

            //int mahd = Int32.Parse(mahopdong);
            int mak = Int32.Parse(makh);
            
            conn.Open();
            //String sql = 
            cmd = new SqlCommand("Update khachhang set khten='" + khten + "', khdiachi = '" + khdiachi + "',khngaysinh= '" + khngaysinh + "',khsdt = '" + khsdt + "',khgioitinh = '" + khgioitinh + "',khemail = '" + khemail + "', khcmt = '" + khcmt + "',khngaytaohoso = '" + khngaytaohoso + "' where makh=" + mak + "", conn);
            da = new SqlDataAdapter(cmd);
            int i = cmd.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds, "khachhang");
            cmb = new SqlCommandBuilder(da);
            //dataGridView1.DataSource = ds.Tables[0];
            // this.frmHopdongvayvon_Load();
            //this.LoadForm();
            this.frmDanhsachkhachhang_Load(sender, e);
            conn.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            tbmkh.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tbtkh.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tbdc.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            dtns.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();

            tbsdt.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            cbgt.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            tbemail.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            tbcmt.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            dtngaytao.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
           // conn.Open();
            cmd = new SqlCommand("SELECT * FROM khachhang", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "khachhang");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbmkh.Text == "" || tbtkh.Text == "" || tbdc.Text == "" || dtns.Text == "" || tbsdt.Text == "" || cbgt.Text == "" || tbemail.Text == "" || tbcmt.Text == "" || dtngaytao.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    tbmkh.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM khachhang", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "khachhang");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["makh"] = tbmkh.Text;
                    newRow["khten"] = tbtkh.Text;
                    newRow["khdiachi"] = tbdc.Text;
                    newRow["khngaysinh"] = dtns.Text;
                    newRow["khgioitinh"] = cbgt.Text;
                    newRow["khemail"] = tbemail.Text;

                    newRow["khsdt"] = tbsdt.Text;
                    newRow["khcmt"] = tbcmt.Text;
                    newRow["khngaytaohoso"] = dtngaytao.Text;
                    dt.Rows.Add(newRow);
                    da.Update(ds, "khachhang");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                tbmkh.Focus();

            } 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "khachhang");
            MessageBox.Show("Xóa thành công");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemma.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemma.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM khachhang where makh  = '" + tbtimkiemma.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "khachhang");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemten.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemten.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM khachhang where khten  = '" + tbtimkiemten.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "khachhang");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       
        
    }
}
